package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.admin.service.ICategeryService;
import com.shop.admin.validate.category.CategeryCreateValidate;
import com.shop.admin.validate.category.CategerySearchValidate;
import com.shop.admin.validate.category.CategeryUpdateValidate;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.vo.category.CategoryDetailVo;
import com.shop.admin.vo.category.CategoryListedVo;
import com.shop.common.core.PageResult;
import com.shop.common.entity.Category;
import com.shop.common.mapper.CategoryMapper;
import com.shop.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 分类实现类
 * @author DragonDean
 */
@Service
public class CategoryServiceImpl implements ICategeryService {
        
    @Resource
    CategoryMapper courseCategoryMapper;

    /**
     * 分类列表
     *
     * @author DragonDean
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CourseCategeryListedVo>
     */
    @Override
    public PageResult<CategoryListedVo> list(PageValidate pageValidate, CategerySearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        courseCategoryMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:type:int",
            "like:name:str",
        });

        IPage<Category> iPage = courseCategoryMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CategoryListedVo> list = new LinkedList<>();
        for(Category item : iPage.getRecords()) {
            CategoryListedVo vo = new CategoryListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 分类详情
     *
     * @author DragonDean
     * @param id 主键参数
     * @return CourseCategery
     */
    @Override
    public CategoryDetailVo detail(Integer id) {
        Category model = courseCategoryMapper.selectOne(
                new QueryWrapper<Category>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CategoryDetailVo vo = new CategoryDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 分类新增
     *
     * @author DragonDean
     * @param createValidate 参数
     */
    @Override
    public void add(CategeryCreateValidate createValidate) {
        Category model = new Category();
        model.setType(createValidate.getType());
        model.setName(createValidate.getName());
        model.setCover(createValidate.getCover());
        model.setBackground(createValidate.getBackground());
        model.setColor(createValidate.getColor());

        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        courseCategoryMapper.insert(model);
    }

    /**
     * 分类编辑
     *
     * @author DragonDean
     * @param updateValidate 参数
     */
    @Override
    public void edit(CategeryUpdateValidate updateValidate) {
        Category model = courseCategoryMapper.selectOne(
                new QueryWrapper<Category>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setCover(updateValidate.getCover());
        model.setBackground(updateValidate.getBackground());
        model.setColor(updateValidate.getColor());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        courseCategoryMapper.updateById(model);
    }

    /**
     * 分类删除
     *
     * @author DragonDean
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Category model = courseCategoryMapper.selectOne(
                new QueryWrapper<Category>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        courseCategoryMapper.updateById(model);
    }

}
